package dang.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import dang.action.cart.CartItem;
import dang.dao.OrderDao;
import dang.entity.Order;
import dang.entity.ReceiveAddress;
import dang.exception.DangException;

public class OrderDaoImpl   implements OrderDao {
	private Connection connection;

	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	@Override
	public void saveAddress(ReceiveAddress receiveAddress) throws DangException {
		String sql="insert into d_receive_address (user_id,receive_name,full_address," +
				"postal_code,mobile,phone) values(?,?,?,?,?,?)";
		try {
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setInt(1, receiveAddress.getUser_id());
			ps.setString(2, receiveAddress.getReceive_name());
			ps.setString(3, receiveAddress.getFull_address());
			ps.setString(4, receiveAddress.getPostal_code());
			ps.setString(5, receiveAddress.getMobile());
			ps.setString(6, receiveAddress.getPhone());
			ps.execute();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DangException(e);
		}
		
	}

	@Override
	public List<ReceiveAddress> findByUserId(int userId) throws DangException {
		String sql="select * from d_receive_address where user_id=?";
		List<ReceiveAddress> receiveAddresses=new ArrayList<ReceiveAddress>();
		try {
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setInt(1, userId);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				ReceiveAddress receiveAddress=new ReceiveAddress();
				receiveAddress.setFull_address(rs.getString("full_address"));
				receiveAddress.setId(rs.getInt("id"));
				receiveAddress.setUser_id(userId);
				receiveAddress.setMobile(rs.getString("mobile"));
				receiveAddress.setPhone(rs.getString("phone"));
				receiveAddress.setPostal_code(rs.getString("postal_code"));
				receiveAddress.setReceive_name(rs.getString("receive_name"));
				receiveAddresses.add(receiveAddress);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DangException(e);
		}
		return receiveAddresses;
	}

	@Override
	public void saveItems(List<CartItem> items,int orderId) throws DangException {
		String sql="insert into d_item (order_id,product_id,product_name,dang_price,product_num,amount) values(?,?,?,?,?,?)";
		try {
			PreparedStatement ps = connection.prepareStatement(sql);
			for (CartItem item : items) {
				if(!item.getProduct().isDeleted()){
					ps.setInt(1, orderId);
					ps.setInt(2, item.getProduct().getId());
					ps.setString(3, item.getProduct().getProduct_name());
					ps.setDouble(4, item.getProduct().getDang_price());
					ps.setInt(5, item.getQty());
					ps.setDouble(6, item.getQty()*item.getProduct().getDang_price());
					ps.execute();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public int saveOrder(Order order) throws DangException {
		String sql = "insert into d_order (user_id,status,order_time,total_price) values(?,?,?,?)";
		int orderId=-1;
		try {
			PreparedStatement ps = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			ps.setInt(1, order.getUser_id());
			ps.setInt(2, 10);//TODO
			ps.setLong(3, new Date().getTime());
			ps.setDouble(4, order.getTotal_price());
			ps.execute();
			ResultSet rs=ps.getGeneratedKeys();
			if(rs.next()){
				orderId=rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DangException(e);
		}
		
		return orderId;
	}

}
